package com.bdqn.controller;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.pojo.Provider;
import com.bdqn.pojo.SmbmsProvider;
import com.bdqn.pojo.SmbmsUser;
import com.bdqn.serice.SmbmsProviderService;
import com.bdqn.utils.Constants;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author www
 * @Description: 供应商控制器
 * @date 2020-06-05 9:13
 */
@Controller
public class ProviderController {
    private Logger log=LogManager.getLogger(ProviderController.class);
    @Resource
    private SmbmsProviderService smbmsProviderService;

    //点击供应商
    @RequestMapping(value = "/queryPro.do")
    public String queryPro(@RequestParam(value="queryProCode",required = false) String queryProCode,
                           @RequestParam(value="queryProName",required = false) String queryProName, Model model){
        log.info("进入供应商查询");
        List<SmbmsProvider> list=smbmsProviderService.selectSmbmsProvider(queryProName,queryProCode);
        model.addAttribute("providerList",list);
        model.addAttribute("queryProCode",queryProCode);
        model.addAttribute("queryProName",queryProName);
        return "providerlist";
    }

    //跳转新增
    @RequestMapping(value = "/toAddPro.do")
    public  String toAddPr(Provider provider){
        log.info("跳转新增");
            return "provideradd";
    }
    //新增
    @RequestMapping(value = "/toAddPro1.do")
    public  String toAddPro(Provider provider, HttpSession session){
        log.info("进入新增"+provider.getProNme()+provider.getProAddress()+provider.getProCode());
        SmbmsProvider smbmsProvider=new SmbmsProvider();
        BeanUtils.copyProperties(provider,smbmsProvider);
        SmbmsUser loginUser= (SmbmsUser) session.getAttribute(Constants.USER_SESSION);
        System.out.println("登录人"+loginUser);
        if (loginUser == null) {
            return "";
        }
        smbmsProvider.setCreatedBy(loginUser.getId());
        smbmsProvider.setCreationDate(new Date());
        int id =smbmsProviderService.insertOrUpdateSelective(smbmsProvider);
        System.out.println(id);
        if(id>0){
            return "redirect:/queryPro.do";
        }else{

            return "provideradd";
        }
    }
    //删除
    @GetMapping("/delProvider")
    @ResponseBody
    public String delProvider(@RequestParam(value="proid",required = false) Long proid){
        log.info("进入删除");
        //deleteByPrimaryKey
        HashMap<String, String> resultMap = new HashMap<String, String>();
        int flag = smbmsProviderService.deleteByPrimaryKey(proid);
        System.out.println(flag+"删除"+proid);
        if(proid>0){
            if(flag>0){//删除成功
                resultMap.put("delResult","true");
            }else{//删除失败
                resultMap.put("delResult","false");
            }
        }else{
            resultMap.put("delResult","notexist");
        }
        String jsonString= JSONObject.toJSONString(resultMap);
        return jsonString;

    }
    //查看
    @RequestMapping("/getProviderById/{url}/{id}")
    public  String getProviderById(@PathVariable(value = "id") Long id, @PathVariable(value = "url") String url, Model model){
       log.info("进入查看"+id);
        SmbmsProvider bill= (SmbmsProvider) smbmsProviderService.selectid(id);
        model.addAttribute("provider",bill);
        return url;
    }
    //修改
    @RequestMapping("/modifyPro.do")
    public String modifyPro(SmbmsProvider smbmsProvider){
        smbmsProvider.setCreationDate(new Date());
        smbmsProvider.setModifyDate(new Date());
        int id=smbmsProviderService.updateByPrimaryKeySelective(smbmsProvider);
        log.info(id+"修改"+smbmsProvider.getProNme());
        if(id>0){
            return "redirect:/queryPro.do";
        }else{
            return "providermodify";
        }
    }
}
